package com.heu.blood.match.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.match.entity.BloodVerifyPatientEntity;
import com.heu.blood.match.service.BloodVerifyPatientService;
import com.heu.blood.match.service.PatientInformationService;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import com.heu.blood.transfusion.service.TransfusionApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:23:13
 */
@Slf4j
@RestController
@RequestMapping("match/bloodverifypatient")
public class BloodVerifyPatientController {
    @Autowired
    private BloodVerifyPatientService bloodVerifyPatientService;

    @Autowired
    private TransfusionApplicationService transfusionApplicationService;

    @Autowired
    private PatientInformationService patientInformationService;

    /**
     * 分页查询病人血样核验信息
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "分页查询病人血样核验信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bloodVerifyPatientService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "条件查询病人血样核验信息")
    public R info(@PathVariable("id") Long id){
        BloodVerifyPatientEntity bloodVerifyPatient = bloodVerifyPatientService.getById(id);

        return R.ok().put("data", bloodVerifyPatient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "保存病人血样核验信息")
    public R save(@RequestBody BloodVerifyPatientEntity bloodVerifyPatient){
        bloodVerifyPatientService.save(bloodVerifyPatient);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "分页查询病人血样核验信息")
    public R update(@RequestBody BloodVerifyPatientEntity bloodVerifyPatient){
        bloodVerifyPatientService.updateById(bloodVerifyPatient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "删除病人血样核验信息")
    public R delete(@RequestBody Long[] ids){
        bloodVerifyPatientService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 通过输血申请单信息来新增对应的病人核验信息
     * @param transfusionApplications
     * @return
     */
    @PostMapping("/saveWithTransfusion")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "通过输血申请单信息来新增对应的病人核验信息")
    public R saveWithTransfusion(@RequestBody List<TransfusionApplicationEntity> transfusionApplications){
        // 通过输血申请单信息来新增对应的病人核验信息
        Boolean result =  bloodVerifyPatientService.saveWithTransfusion(transfusionApplications);
        if (!result){
            // 代表没有数据需要保存
            return R.ok("所有数据已最新");
        }
        return R.ok("新增数据已保存");
    }

    /**
     * 患者血型复查
     * @param bloodVerifyPatient
     * @return
     */
    @PostMapping("/verify")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "患者血型复查")
    public R verify(@RequestBody BloodVerifyPatientEntity bloodVerifyPatient) {
        // 通过比对患者信息表和病人核验表中的血液信息来填充核验结果
        Pair<Boolean, String> res = bloodVerifyPatientService.verify(bloodVerifyPatient);
        if (res.getLeft()){
            return R.ok(res.getRight());
        } else {
            return R.error(res.getRight());
        }
    }

    /**
     * 条件查询患者核验表信息
     * @param bloodVerifyPatientEntity
     * @return
     */
    @RequestMapping("/query")
    @ControllerLogAnnotation(module = "病人信息校验", operate = "条件查询患者核验表信息")
    public R query(@RequestBody BloodVerifyPatientEntity bloodVerifyPatientEntity){
        List<BloodVerifyPatientEntity> bloodVerifyPatients = bloodVerifyPatientService.query(bloodVerifyPatientEntity);
        if (bloodVerifyPatients == null || bloodVerifyPatients.isEmpty()){
            return R.error("未查询到符合要求的核验表信息");
        }
        return R.ok().put("list", bloodVerifyPatients);
    }

}
